# Virtual Router Simulator 
#
# Copyright (C) 2009-2010 VRSim Project
# Author: Choonho Son <choonho@kt.com>
# URL: <http://vrsim.googlecode.com>
# For license information, see LICENSE.TXT
#

import VRSim.Globals as globals
from PyQt4 import QtGui

class NodesSummary(QtGui.QTreeWidget):
    def __init__(self,  parent):
        QtGui.QTreeWidget.__init__(self,  parent)
        self.header().show()
        self.setRootIsDecorated(True)
        
        
        self.headerName = ["Index", "Hostname",  "State",  "Virtual Node",  "VM IP",  "VM Port"]
        self.setHeaderLabels(self.headerName)
        self.setColumnWidth(0,  50)
        
    def refresh(self):
        self.clear()
        
        self.setHeaderLabels(self.headerName)
        self.setColumnWidth(0,  50)
        count = 1 
        for node in globals.GApp.topology.nodes.itervalues():
            rootitem = QtGui.QTreeWidgetItem(self)
            hostname = node.physnode.hostname
            if type(hostname) != unicode:
                hostname = unicode(hostname)
                
            rootitem.setText(0,  str(count))
            rootitem.setText(1,  hostname)
            rootitem.setText(2,  str(node.physnode.state))
            if node.isVirtual == True:
                vstr = "Yes"
                # VM IP, Port
                ip = node.physnode.devices.console_ip
                port = node.physnode.devices.console
                rootitem.setText(4,  ip)
                rootitem.setText(5,  str(port))
            else:
                vstr = "No"
            rootitem.setText(3,  vstr)
            
            count += 1
        
            self.insertTopLevelItem(0,  rootitem)
        
